package com.mallcai.bigdata.ladon.dpl.mapper.report.provider;

import com.google.common.base.Joiner;
import com.mallcai.bigdata.ladon.dpl.entity.report.Group;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class GroupSqlProvider extends BaseSqlProvider<Group> {

    public static final String TB_GROUP = "r_group";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_GROUP;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("parent_id", "#{parentId}");
        map.put("name", "#{name}");
        map.put("icon", "#{icon}");
        map.put("level", "#{level}");
        map.put("sort_id", "#{sortId}");
        map.put("comment", "#{comment}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("parentId", "parent_id");
        map.put("name", "name");
        map.put("icon", "icon");
        map.put("level", "level");
        map.put("sortId", "sort_id");
        map.put("comment", "comment");
        return map;
    }

    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_g.archive = 0");
        if (params.containsKey("name") && params.get("name") != null) {
            sb.append(" and tb_g.name like CONCAT('%', #{params.name}, '%')");
        }
        if (params.containsKey("parentId") && StringUtils.isNotBlank(params.get("parentId").toString())) {
            sb.append(" and tb_g.parent_id = #{params.parentId}");
        }
        return new SQL() {
            {
                SELECT("tb_g.*");
                FROM(getTableName() + " tb_g");
                WHERE(sb.toString());
                ORDER_BY(" parent_id, sort_id");
            }
        }.toString();
    }

    /**
     * 判断是否有相同的名称
     */
    public String hasSameName(@Param("parentId") Long parentId, @Param("name") String name, @Param("filterId") Long filterId) {
        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0")
                .append(" AND parent_id = #{parentId}")
                .append(" AND name = #{name}");
        if (filterId != null) {
            sb.append(" AND id != #{filterId}");
        }

        return new SQL() {
            {
                SELECT("count(0)");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }

    /**
     * 获取子节点
     */
    public String getChildrenByPid(@Param("parentId") Long parentId) {
        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0")
                .append(" AND parent_id = #{parentId}");

        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }

    /**
     * 获取子节点
     */
    public String getChildrenByPids(@Param("parentIds") List<Long> parentIds) {
        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0")
                .append(" AND parent_id IN (")
                .append(Joiner.on(",").join(parentIds))
                .append(")");

        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }

    /**
     * 判断是否存在子节点(报表组或报表)
     */
    public String hasChildren(@Param("parentId") Long parentId) {
        return new SQL() {
            {
                SELECT("SUM(size)");
                FROM("( " +
                        " SELECT count(0) as size FROM " + TB_GROUP + " WHERE archive = 0 AND parent_id = #{parentId} " +
                        " UNION ALL " +
                        " SELECT count(0) as size FROM " + ReportRlGroupSqlProvider.TB_REPORT_RL_GROUP + " WHERE archive = 0 AND group_id = #{parentId} " +
                        " ) as tb_tmp");
            }
        }.toString();
    }
}
